package com.five.taotao.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.five.taotao.Application.MyApplication;
import com.five.taotao.R;
import com.five.taotao.entity.Product;
import com.five.taotao.entity.ShoppingCart;
import com.five.taotao.product_details.ProductDetailsActivity;


import java.util.List;

/**
 * Created by gzq on 2016/11/7.
 */
public class ShopcarAdapter extends BaseAdapter{
    private List<ShoppingCart.ListBean> mData;
    private Context context;
    private ShoppingCart.ListBean product;
    //    private List<Product.ListBean> newData;
    private ShopcarListener shopcarListener;
    private View view;
    public void setOnShopcarListener(ShopcarListener shopcarListener){
        this.shopcarListener=shopcarListener;
    }
    public ShopcarAdapter(List<ShoppingCart.ListBean> mData,Context context){
        this.mData=mData;
        this.context=context;
//        newData=new ArrayList<>();
    }
    @Override
    public int getCount() {
        return this.mData.size();
    }

    @Override
    public Object getItem(int i) {
        return this.mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int posion, View view, final ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if(view==null){
            view= LayoutInflater.from(this.context).inflate(R.layout.shopcar_list,null,false);
            this.view=view;
            viewHolder=new ViewHolder();
            viewHolder.img= (ImageView) view.findViewById(R.id.iv_shopcar_img);
            viewHolder.title= (TextView) view.findViewById(R.id.tv_shopcar_title);
            viewHolder.vipprice= (TextView) view.findViewById(R.id.tv_shopcar_vipprice);
            viewHolder.price= (TextView) view.findViewById(R.id.tv_shopcar_price);
            viewHolder.count= (TextView) view.findViewById(R.id.tv_shopcar_count);
            viewHolder.count1= (TextView) view.findViewById(R.id.tv_shopcar_count1);
            viewHolder.cost= (TextView) view.findViewById(R.id.tv_shopcar_cost);
            viewHolder.store= (CheckBox) view.findViewById(R.id.cb_shopcar_store);
            viewHolder.goods= (CheckBox) view.findViewById(R.id.cb_shopcar_goods);
            viewHolder.dec= (ImageView) view.findViewById(R.id.iv_shopcar_decGoods);
            viewHolder.inc= (ImageView) view.findViewById(R.id.iv_shopcar_incGoods);
            viewHolder.end= (LinearLayout) view.findViewById(R.id.ll_shopcar_end);
            view.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) view.getTag();

        }
        shopcarListener.select(viewHolder,this.view,posion);
        shopcarListener.decGoods(viewHolder, this.view, posion);
        shopcarListener.incGoods(viewHolder, this.view, posion);

        product=this.mData.get(posion);
        Glide.with(context)
                .load(product.getUrl())
                .into(viewHolder.img);
        viewHolder.title.setText(product.getName());
        viewHolder.vipprice.setText("￥" + product.getVipprice());
        viewHolder.price.setText("￥" + product.getPrice());
        viewHolder.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        viewHolder.count.setText(product.getNum() + "");
        viewHolder.count1.setText("已选" + product.getNum() + "件商品，合计：");
        viewHolder.cost.setText("￥"+product.getVipprice()*product.getNum()+"");

        final Product.ListBean data = new Product.ListBean(product.getName(),product.getPrice(),
                product.getIntroduce(),product.getUrl(),product.getVipprice()
                ,(Product.ListBean.Sid1Bean)product.getSid1(),
                (Product.ListBean.Pid1Bean)product.getPid1()
                ,product.getStock(),product.getProductId()+"");

        viewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProductDetailsActivity.class);
                intent.putExtra("product",data);
                context.startActivity(intent);
            }
        });
        viewHolder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProductDetailsActivity.class);
                intent.putExtra("product",data);
                context.startActivity(intent);
            }
        });

        return view;
    }


    public class ViewHolder{
        public ImageView img;
        public TextView title;
        public TextView vipprice;
        public TextView price;
        public TextView count;
        public TextView cost;
        public CheckBox store;
        public CheckBox goods;
        public ImageView dec;
        public ImageView inc;
        public TextView count1;
        public LinearLayout end;
    }

    public interface ShopcarListener{
        void select(ViewHolder viewHolder,View view,int postion);
        void decGoods(ViewHolder viewHolder,View view,int postion);
        void incGoods(ViewHolder viewHolder,View view,int postion);
    }
}
